package homework.three;



import java.io.*;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner( System.in );
        System.out.print("请输入一个文件名:");
        String fileName = input.next();

        File file = new File( "data\\test1.txt" );
        String result = null;
        try {
            judge( file, fileName );
        }catch (RuntimeException e){
            result = e.getMessage();
        }
        if (result != null){
            File files = new File( result );
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append( files.getParent() );
            stringBuilder.append( "\\copy_");
            stringBuilder.append( files.getName() );
            String results = stringBuilder.toString();
            File newFile = new File( results );

            FileWriter fileWriter = null;
            FileReader fileReader = null;
            BufferedWriter bufferedWriter = null;
            BufferedReader bufferedReader = null;

            try {
                files.createNewFile();
                //字符输出流
                fileWriter = new FileWriter( newFile ,true);
                bufferedWriter =  new BufferedWriter(fileWriter);
                //字符输入流
                fileReader = new FileReader( files );
                bufferedReader = new BufferedReader( fileReader );
                String readLine = bufferedReader.readLine();

                while (readLine != null){
                    bufferedWriter.write( readLine );
                    bufferedWriter.newLine();
                    bufferedWriter.flush();
                    readLine = bufferedReader.readLine();
                }
                System.out.println("文件地址:"+results);
                System.out.println("文件拷贝成功！！！");
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (fileWriter != null){
                    try {
                        fileWriter.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fileReader != null){
                    try {
                        fileReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (bufferedWriter != null){
                    try {
                        bufferedWriter.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (bufferedReader != null){
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }else {
            System.out.println("对不起！您输入的文件名在磁盘中不存在！！！");
        }
    }

    //  查找系统中是否存在此文件
    public static void judge(File file, String fileName){
        File[] files = file.listFiles();
        if (files != null){
            for (File file1 : files) {
                judge( file1,fileName);
            }
        }
        String name = file.getName();
        if (fileName.equals( name )){
            throw new RuntimeException( file.getAbsolutePath() );
        }
    }
}
